home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10321 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  64 lines

  1. Newsgroups: comp.lang.c++,rb.technical
  2. Path: yama.mcc.ac.uk!liv!news
  3. From: nickc@liv.ac.uk (Spider plant breeding program)
  4. Subject: What is "placement new" - was Re: Can copy constructor and operator= share code?
  5. In-Reply-To: Frederic LACHASSE's message of Sat, 02 Mar 1996 23:18:20 +0000
  6. Message-ID: <NICKC.96Mar7142042@uxe.liv.ac.uk>
  7. Sender: news@liverpool.ac.uk (News System)
  8. Nntp-Posting-Host: uxe.liv.ac.uk
  9. Organization: M-6 M-4 M-x hanoi
  10. References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <VA.00000053.00cdab05@fred>
  11. Date: Thu, 7 Mar 1996 14:20:42 GMT
  12.  
  13. In article <VA.00000053.00cdab05@fred> Frederic LACHASSE <lachass@worldnet.fr> writes:
  14.  
  15. > From: Frederic LACHASSE <lachass@worldnet.fr>
  16. > Generally, the operator=() must release old resource and create a copy 
  17. > of the other object. So a generic operator=() can be:
  18. > T &T::operator =(const T &t)
  19. > {
  20. >   if (this != &t) // if objects are same, nothing to do
  21. >   {
  22. >     ~T(); // explicit call to destructor to release resources.
  23.  
  24. Shouldn't this be    this->~T();
  25.  
  26. >     new(this) T(t); // use of the placement operator to call the
  27. >                     // copy constructor.
  28. >   }
  29.  
  30.             return *this;
  31.  
  32. > }
  33. > Some times though, the assignement operator can be optimize to reuse 
  34. > resources of the old object.
  35.  
  36. What is ths placement new operator?
  37.  
  38. It's mentioned in passing in the FAQ, but never defined.
  39.  
  40. No compiler I can run recognises this syntax
  41. (I've tried gcc 2.6.3, Solaris C++ (V4 I think), Irix C++)
  42.  
  43. The problem I'm trying to solve is:
  44.  
  45. class a
  46. {
  47.   int *i;
  48.   const double &d;    // Actually something more complex
  49.  
  50.   public:
  51.   a( const double &d_ ) : i( new int[3] ), d( d_ ) {};
  52.   ~a() { delete i; };
  53. };
  54.  
  55. How do I write operator = ( const class &a )    ?
  56.  
  57. Thanks in advance,
  58.  
  59. Nicholas Clark
  60. -- 
  61. C:\> ECHO f 0000:0000 ffff 66 | DEBUG
  62.